cmake_minimum_required(VERSION 3.14)
project(aicpu_engine_common)
if (BUILD_OPEN_PROJECT)
    get_filename_component(TOP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../.." ABSOLUTE)
    set(PROTO_PATH ${TOP_DIR}/third_party/metadef)
else()
    get_filename_component(TOP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../.." ABSOLUTE)
    set(PROTO_PATH ${TOP_DIR}/metadef)
endif()
set(BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

set(proto_src_files
    ${PROTO_PATH}/proto/task.proto
)

protobuf_generate(aicpu PROTO_SRCS PROTO_HDRS ${proto_src_files})

set(local_lib_src_files
    ${CMAKE_CURRENT_SOURCE_DIR}/engine/base_engine.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_ops_kernel_info_store/aicpu_ops_kernel_info_store.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_ops_kernel_info_store/kernel_info.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_graph_optimizer/aicpu_graph_optimizer.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_graph_optimizer/optimizer.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_graph_optimizer/graph_optimizer_utils.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/config/config_file.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_ops_kernel_builder/aicpu_ops_kernel_builder.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_ops_kernel_builder/kernel_builder.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/config/ops_json_file.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/error_code/error_code.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/util/util.cc
    ${PROTO_HDRS}
)

set(local_lib_builder_src_files
    ${CMAKE_CURRENT_SOURCE_DIR}/aicpu_ops_kernel_builder/kernel_builder.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/config/config_file.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/config/ops_json_file.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/error_code/error_code.cc
    ${CMAKE_CURRENT_SOURCE_DIR}/util/util.cc
    ${PROTO_HDRS}
)

if (BUILD_OPEN_PROJECT)
  set(local_lib_inc_path
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${TOP_DIR}/third_party/metadef
    ${TOP_DIR}/third_party/metadef/inc
    ${TOP_DIR}/third_party/metadef/inc/external
    ${TOP_DIR}/third_party/inc/external
    ${TOP_DIR}/third_party/inc
    ${TOP_DIR}/third_party/inc/framework
    ${TOP_DIR}/third_party/inc/ops
    ${C_SEC_INCLUDE}
    ${JSON_INCLUDE}
    ${Protobuf_INCLUDE}
    ${AIR_ROOT}/third_party/metadef/cpu_context/inc
    ${CMAKE_BINARY_DIR}/proto/aicpu
    ${CMAKE_BINARY_DIR}/proto/aicpu/proto
  )
else()
  set(local_lib_inc_path
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${TOP_DIR}/inc
    ${TOP_DIR}/metadef
    ${TOP_DIR}/metadef/inc
    ${TOP_DIR}/graphengine/inc
    ${TOP_DIR}/inc/external
    ${TOP_DIR}/libc_sec/include
    ${TOP_DIR}/metadef/inc/external
    ${TOP_DIR}/graphengine/inc/external
    ${TOP_DIR}/graphengine/inc/framework
    ${TOP_DIR}/third_party/json/include
    ${TOP_DIR}/open_source/json/include
    ${TOP_DIR}/cann/ops/built-in/op_proto/inc
    ${TOP_DIR}/asl/ops/cann/ops/built-in/op_proto/inc
    ${CMAKE_BINARY_DIR}/proto/aicpu
    ${CMAKE_BINARY_DIR}/proto/aicpu/proto
  )
endif()


add_library(aicpu_engine_common SHARED
    ${local_lib_src_files}
)

add_library(aicpu_builder_common SHARED
    ${local_lib_builder_src_files}
)

add_library(atc_aicpu_engine_common SHARED
    ${local_lib_src_files}
)

add_dependencies(aicpu_engine_common json)

add_dependencies(aicpu_builder_common json)

add_dependencies(atc_aicpu_engine_common json)
##-----aicpu_engine_common

if (BUILD_OPEN_PROJECT)
target_link_libraries(aicpu_engine_common PRIVATE
    c_sec
    slog
    error_manager
    graph
    platform
    -lrt
    -ldl
    $<BUILD_INTERFACE:intf_pub>
)

target_link_libraries(aicpu_builder_common PRIVATE
    #动态库快关（把动态库都包起来）
    -Wl,--no-as-needed
    c_sec
    slog
    error_manager
    graph
    -Wl,--as-needed
    #静态库（把静态库都包起来）
    -Wl,--whole-archive
    -Wl,--no-whole-archive
    $<$<NOT:$<STREQUAL:${TARGET_SYSTEM_NAME},Android>>:-lrt>
    -ldl
    $<BUILD_INTERFACE:intf_pub>
)

target_link_libraries(atc_aicpu_engine_common PRIVATE
    c_sec
    slog
    graph
    error_manager
    platform
    -lrt
    -ldl
    $<BUILD_INTERFACE:intf_pub>
)
else()
target_link_libraries(aicpu_engine_common PRIVATE
    -Wl,--no-as-needed
    ascend_protobuf_inc
    -Wl,--as-needed
    c_sec
    slog
    error_manager
    graph
    platform
    -lrt
    -ldl
    $<BUILD_INTERFACE:intf_pub>
)

target_link_libraries(aicpu_builder_common PRIVATE
    #动态库快关（把动态库都包起来）
    -Wl,--no-as-needed
    ascend_protobuf_inc
    c_sec
    slog
    error_manager
    graph
    -Wl,--as-needed
    #静态库（把静态库都包起来）
    -Wl,--whole-archive
    -Wl,--no-whole-archive
    $<$<NOT:$<STREQUAL:${TARGET_SYSTEM_NAME},Android>>:-lrt>
    -ldl
    $<BUILD_INTERFACE:intf_pub>
)

target_link_libraries(atc_aicpu_engine_common PRIVATE
    -Wl,--no-as-needed
    ascend_protobuf_inc
    -Wl,--as-needed
    c_sec
    slog
    graph
    error_manager
    platform
    -lrt
    -ldl
    $<BUILD_INTERFACE:intf_pub>
)
endif()

##### target atclib/aicpu_engine_common
target_include_directories(aicpu_engine_common PRIVATE
     ${local_lib_inc_path}
)

target_include_directories(aicpu_builder_common PRIVATE
     ${local_lib_inc_path}
)

target_include_directories(atc_aicpu_engine_common PRIVATE
    ${local_lib_inc_path}
)

target_compile_definitions(aicpu_engine_common PRIVATE
    AICPU_PLUGIN
)

target_compile_definitions(aicpu_builder_common PRIVATE
    AICPU_PLUGIN
)

target_compile_definitions(atc_aicpu_engine_common PRIVATE
    AICPU_PLUGIN
)

target_compile_options(aicpu_engine_common PRIVATE
    -std=c++11
    -Wno-deprecated-declarations
    -fno-common
    -fno-strict-aliasing
)

target_compile_options(aicpu_builder_common PRIVATE
    -std=c++11
    -Wno-deprecated-declarations
    -fno-common
    -fno-strict-aliasing
)

target_compile_options(atc_aicpu_engine_common PRIVATE
    -std=c++11
    -Wno-deprecated-declarations
    -fno-common
    -fno-strict-aliasing
)

set(INSTALL_LIBRARY_DIR lib)
set(atc_aicpu_engine_common_path ${CMAKE_CURRENT_BINARY_DIR}/libatc_aicpu_engine_common.so)
set(aicpu_builder_common ${CMAKE_CURRENT_BINARY_DIR}/libaicpu_builder_common.so)

set_target_properties(atc_aicpu_engine_common
    PROPERTIES
    OUTPUT_NAME aicpu_engine_common
    LIBRARY_OUTPUT_DIRECTORY ${INSTALL_LIBRARY_DIR}/atclib
)

set_target_properties(aicpu_builder_common
    PROPERTIES
    OUTPUT_NAME aicpu_builder_common
)

install(TARGETS aicpu_engine_common OPTIONAL
    EXPORT aicpu_engine_common-targets
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}
    RUNTIME DESTINATION ${INSTALL_RUNTIME_DIR}
)

install(TARGETS aicpu_builder_common OPTIONAL
    EXPORT aicpu_builder_common-targets
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}
    RUNTIME DESTINATION ${INSTALL_RUNTIME_DIR}
)

install(TARGETS atc_aicpu_engine_common OPTIONAL
    EXPORT atc_aicpu_engine_common-targets
    LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}/atclib
    RUNTIME DESTINATION ${INSTALL_RUNTIME_DIR}/atclib
)

set(init.conf_src_path  ${CMAKE_CURRENT_SOURCE_DIR}/config/init.conf)
install(FILES ${init.conf_src_path}
    DESTINATION ${INSTALL_LIBRARY_DIR}
    OPTIONAL
)
